package com.fy56.platform.logistics.service.impl.common;

import com.fy56.platform.logistics.common.mapper.LogisticsBasisMapper;
import com.fy56.platform.logistics.common.BaseEntity;
import com.fy56.platform.logistics.common.Page;
import com.fy56.platform.logistics.service.common.ILogisticsBasisService;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 基础Service
 *
 * @author kangqi
 * @version 2016-11-11
 */
@Service
@Transactional(readOnly = true)
public class LogisticsBasisServiceImpl implements ILogisticsBasisService {
    @Autowired
    private LogisticsBasisMapper logisticsBasisMapper;
    @Override
    public Page<Map<String, Object>> findPage(BaseEntity t, Integer page, Integer pageSize) {
        if(StringUtil.isEmpty(t.getColumn_())){
            t.setColumn_(" * ");
        }
        if (page==null || page == 0){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 0;
        }
        Page<Map<String, Object>> result = new Page<Map<String, Object>>();
        if (pageSize > 0) {
            //需要分页
            result.setPage(page);
            result.setPageSize(pageSize);
            page--;
            t.setStartIndex_(page * pageSize);
            t.setEndIndex_(pageSize);
            result.setRecords(logisticsBasisMapper.findCount(t));
            if (result.getRecords() == null || result.getRecords() == 0) {
                return result.init();//无数据
            }
        }
        result.setRows(logisticsBasisMapper.findPage(t));
        if (pageSize == 0) {//不分页,总记录数直接取rows
            result.setRecords(result.getRows().size());
        }
        if (t.getSum_() != null && !t.getSum_().equals("") && result.getRows().size() > 0) {
            result.setListSum(logisticsBasisMapper.findSum(t));//合计
        }
        return result.init();
    }
    @Override
    public List<Map<String, Object>> queryStatistic(Map<String,Object> param) {
        return logisticsBasisMapper.queryStatistic(param);
    }
    @Override
    public Integer queryStatisticCount(Map<String,Object> param) {
        return logisticsBasisMapper.queryStatisticCount(param);
    }
}
